<template>
  <div class="home-page">
    <section class="home-hero">
      <div class="hero__intro">
        <span class="hero__tag">校园文化中心</span>
        <h1 class="hero__title">欢迎回到校园文化管理平台</h1>
        <p class="hero__desc">
          这里汇聚校园最新动态、公告通告与文化活动。通过快捷入口高效切换所需模块，及时掌握校园脉动，为校园文化运营、服务保障提供支持。
        </p>
        <div class="hero__summary">
          <div class="summary-item">
            <span class="summary-item__label">今日看点</span>
            <span class="summary-item__value">{{ carouselNews.length || 0 }}</span>
            <span class="summary-item__hint">条新闻上新</span>
          </div>
          <div class="summary-item">
            <span class="summary-item__label">快捷入口</span>
            <span class="summary-item__value">{{ navItems.length }}</span>
            <span class="summary-item__hint">个常用功能</span>
          </div>
        </div>
      </div>

      <el-card class="hero__carousel" shadow="hover" v-loading="newsLoading">
        <template v-if="carouselNews.length">
          <el-carousel
            height="320px"
            indicator-position="outside"
            trigger="hover"
            arrow="hover"
            :interval="6000"
            class="carousel"
          >
            <el-carousel-item
              v-for="item in carouselNews"
              :key="item.newsId"
              class="carousel__item"
            >
              <div class="news-slide" @click="goNewsDetail(item)">
                <div
                  class="news-slide__image"
                  :class="{ 'news-slide__image--fallback': !resolveImage(item.image) }"
                  :style="resolveImage(item.image) ? { backgroundImage: `url(${resolveImage(item.image)})` } : {}"
                >
                  <div class="news-slide__overlay"></div>
                </div>
                <div class="news-slide__content">
                  <span class="news-slide__tag">今日新闻</span>
                  <h3 class="news-slide__title">{{ item.newsTitle }}</h3>
                  <p class="news-slide__excerpt">{{ getExcerpt(item.content) }}</p>
                  <div class="news-slide__meta">
                    <span>{{ item.name || "未知发布人" }}</span>
                    <span>{{ formatNewsTime(item.newsTime) }}</span>
                  </div>
                </div>
              </div>
            </el-carousel-item>
          </el-carousel>
        </template>
        <el-empty v-else description="暂无新闻内容" />
      </el-card>
    </section>

    <section class="home-shortcuts">
      <div class="section-header">
        <h2 class="section-title">校园服务快捷入口</h2>
        <p class="section-subtitle">常用模块一站直达，快速处理日常管理与文化传播工作。</p>
      </div>
      <el-row :gutter="20" class="shortcut-grid">
        <el-col
          v-for="item in navItems"
          :key="item.key"
          :xs="24"
          :sm="12"
          :md="8"
          :lg="8"
          :xl="4"
        >
          <div class="shortcut-card" :style="{ '--card-accent': item.color }" @click="goPage(item.route)">
            <div class="shortcut-card__icon">
              <el-icon :size="28">
                <component :is="item.icon" />
              </el-icon>
            </div>
            <div class="shortcut-card__content">
              <h3>{{ item.title }}</h3>
              <p>{{ item.desc }}</p>
            </div>
            <el-icon class="shortcut-card__arrow">
              <ArrowRight />
            </el-icon>
          </div>
        </el-col>
      </el-row>
    </section>

    <section class="home-news-brief" v-if="carouselNews.length">
      <div class="section-header">
        <h2 class="section-title">热点新闻速览</h2>
        <p class="section-subtitle">精选新闻摘要，帮助你快速了解校园重点资讯。</p>
      </div>
      <el-row :gutter="20">
        <el-col
          v-for="item in carouselNews"
          :key="`brief-${item.newsId}`"
          :xs="24"
          :sm="12"
          :lg="8"
        >
          <el-card class="news-brief-card" shadow="hover" @click="goNewsDetail(item)">
            <div class="news-brief-card__top">
              <span class="news-brief-card__tag">新闻</span>
              <span class="news-brief-card__time">{{ formatNewsTime(item.newsTime) }}</span>
            </div>
            <h3 class="news-brief-card__title">{{ item.newsTitle }}</h3>
            <p class="news-brief-card__excerpt">{{ getExcerpt(item.content, 60) }}</p>
            <div class="news-brief-card__footer">
              <span>{{ item.name || "未知发布人" }}</span>
              <el-icon>
                <ArrowRight />
              </el-icon>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </section>
  </div>
</template>

<script setup name="Index">
import { ref, onMounted } from "vue"
import { useRouter } from "vue-router"
import { listNews } from "@/api/manage/news"
import { parseTime } from "@/utils/ruoyi"
import { isExternal } from "@/utils/validate"
import {
  Reading,
  Bell,
  Box,
  Calendar,
  Collection,
  MapLocation,
  ArrowRight
} from "@element-plus/icons-vue"

const router = useRouter()
const carouselNews = ref([])
const newsLoading = ref(false)

const navItems = [
  {
    key: "news",
    title: "新闻资讯",
    desc: "了解校园最新要闻与活动报道",
    route: "/news",
    color: "#3f8cff",
    icon: Reading
  },
  {
    key: "announce",
    title: "公告通知",
    desc: "及时掌握校园公告与通知动态",
    route: "/announce",
    color: "#ff7043",
    icon: Bell
  },
  {
    key: "lost",
    title: "失物招领",
    desc: "查找或发布校园遗失物品信息",
    route: "/lost",
    color: "#34c759",
    icon: Box
  },
  {
    key: "holiday",
    title: "节假日安排",
    desc: "查看校历与假期安排信息",
    route: "/holiday",
    color: "#f6c21c",
    icon: Calendar
  },
  {
    key: "history",
    title: "校史档案",
    desc: "回顾校园发展历程与人物故事",
    route: "/history",
    color: "#8b5cf6",
    icon: Collection
  },
  {
    key: "map",
    title: "校园地图",
    desc: "掌握校园地标与功能区域分布",
    route: "/map",
    color: "#00b8d4",
    icon: MapLocation
  }
]

function fetchNews() {
  newsLoading.value = true
  listNews({ pageNum: 1, pageSize: 6 })
    .then(response => {
      carouselNews.value = response.rows || []
    })
    .finally(() => {
      newsLoading.value = false
    })
}

function resolveImage(src) {
  if (!src) return ""
  const [first] = String(src).split(",")
  if (isExternal(first)) {
    return first
  }
  return `${import.meta.env.VITE_APP_BASE_API}${first}`
}

function getExcerpt(content, limit = 90) {
  if (!content) return "暂无内容"
  const text = content.replace(/<[^>]+>/g, "").replace(/&nbsp;/gi, " ").trim()
  return text.length > limit ? `${text.slice(0, limit)}...` : text
}

function formatNewsTime(time) {
  if (!time) return "未知时间"
  return parseTime(time, "{y}-{m}-{d}")
}

function goNewsDetail(item) {
  router.push({
    path: "/news",
    query: { highlight: item.newsId }
  })
}

function goPage(route) {
  if (!route) return
  router.push(route)
}

onMounted(() => {
  fetchNews()
})
</script>

<style scoped lang="scss">
.home-page {
  min-height: 100%;
  padding: 24px;
  background: #f5f7fb;
  display: flex;
  flex-direction: column;
  gap: 32px;
  box-sizing: border-box;
}

.home-hero {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
  gap: 28px;
  background: linear-gradient(135deg, #e5f2ff 0%, #f3f9ff 45%, #ffffff 100%);
  border-radius: 28px;
  padding: 32px 36px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 24px 48px rgba(63, 140, 255, 0.12);
}

.hero__intro {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 18px;
  color: #0f1a2a;
  z-index: 1;
}

.hero__tag {
  display: inline-flex;
  align-items: center;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(63, 140, 255, 0.12);
  color: #306fe3;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.hero__title {
  font-size: 32px;
  font-weight: 700;
  margin: 0;
  line-height: 1.4;
}

.hero__desc {
  margin: 0;
  font-size: 15px;
  line-height: 1.8;
  color: #31435c;
}

.hero__summary {
  display: flex;
  gap: 18px;
  margin-top: 12px;
}

.summary-item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  background: rgba(255, 255, 255, 0.65);
  border-radius: 16px;
  padding: 12px 18px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.summary-item__label {
  font-size: 13px;
  color: #637089;
}

.summary-item__value {
  font-size: 26px;
  font-weight: 700;
  color: #1b3f9b;
}

.summary-item__hint {
  font-size: 12px;
  color: #90a1c0;
}

.hero__carousel {
  border-radius: 22px;
  overflow: hidden;
  border: none;
  box-shadow: 0 16px 32px rgba(71, 117, 187, 0.15);
}

.carousel {
  --el-carousel-indicator-height: 10px;
}

.carousel__item {
  display: flex;
  height: 100%;
}

.news-slide {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
  height: 100%;
  cursor: pointer;
  border-radius: 22px;
  overflow: hidden;
}

.news-slide__image {
  position: relative;
  background-size: cover;
  background-position: center;
  min-height: 320px;
}

.news-slide__image--fallback {
  background: linear-gradient(135deg, #4f83ff 0%, #93b5ff 100%);
}

.news-slide__overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(160deg, rgba(12, 25, 53, 0.65) 0%, rgba(16, 38, 82, 0.2) 80%);
}

.news-slide__content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 28px 30px;
  background: #ffffff;
}

.news-slide__tag {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(63, 140, 255, 0.14);
  color: #356de8;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 12px;
}

.news-slide__title {
  margin: 0 0 16px;
  font-size: 22px;
  line-height: 1.5;
  color: #10203d;
  font-weight: 700;
}

.news-slide__excerpt {
  margin: 0 0 24px;
  color: #55627d;
  font-size: 15px;
  line-height: 1.8;
}

.news-slide__meta {
  display: flex;
  justify-content: space-between;
  color: #9aa6bd;
  font-size: 13px;
}

.home-shortcuts {
  background: #ffffff;
  border-radius: 24px;
  padding: 28px 32px 36px;
  box-shadow: 0 20px 36px rgba(15, 35, 95, 0.08);
}

.section-header {
  margin-bottom: 24px;
}

.section-title {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
  color: #14213d;
}

.section-subtitle {
  margin: 12px 0 0;
  color: #64748b;
  font-size: 14px;
}

.shortcut-grid {
  margin-top: 8px;
}

.shortcut-card {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 20px 20px;
  border-radius: 18px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(244, 248, 255, 0.95) 100%);
  border: 1px solid rgba(229, 235, 245, 0.8);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  box-shadow: 0 8px 20px rgba(15, 52, 96, 0.08);
}

.shortcut-card::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 18px;
  border: 2px solid transparent;
  transition: border-color 0.2s ease;
}

.shortcut-card__icon {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(63, 140, 255, 0.08);
  color: var(--card-accent);
  flex-shrink: 0;
}

.shortcut-card__content {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: #1c2b45;
}

.shortcut-card__content h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.shortcut-card__content p {
  margin: 0;
  color: #6b7a99;
  font-size: 13px;
  line-height: 1.6;
}

.shortcut-card__arrow {
  position: absolute;
  right: 18px;
  bottom: 18px;
  color: rgba(28, 43, 69, 0.35);
  transition: transform 0.2s ease, color 0.2s ease;
}

.shortcut-card:hover {
  transform: translateY(-6px);
  box-shadow: 0 16px 32px rgba(63, 140, 255, 0.18);
}

.shortcut-card:hover::before {
  border-color: rgba(63, 140, 255, 0.18);
}

.shortcut-card:hover .shortcut-card__arrow {
  transform: translateX(6px);
  color: var(--card-accent);
}

.home-news-brief {
  background: #ffffff;
  border-radius: 24px;
  padding: 28px 32px 36px;
  box-shadow: 0 16px 30px rgba(18, 38, 90, 0.08);
}

.news-brief-card {
  border: none;
  border-radius: 18px;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
  cursor: pointer;
}

.news-brief-card__top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: #74829f;
}

.news-brief-card__tag {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(63, 140, 255, 0.12);
  color: #356de8;
  font-weight: 600;
}

.news-brief-card__title {
  margin: 0;
  font-size: 18px;
  line-height: 1.5;
  color: #14213d;
}

.news-brief-card__excerpt {
  margin: 0;
  color: #5d6c88;
  font-size: 14px;
  line-height: 1.7;
  flex: 1;
}

.news-brief-card__footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #8494b5;
  font-size: 13px;
}

@media (max-width: 1200px) {
  .home-hero {
    grid-template-columns: 1fr;
  }

  .hero__summary {
    flex-wrap: wrap;
  }

  .news-slide {
    grid-template-columns: 1fr;
  }

  .news-slide__image {
    min-height: 220px;
  }

  .hero__carousel {
    box-shadow: 0 12px 24px rgba(71, 117, 187, 0.12);
  }
}

@media (max-width: 768px) {
  .home-page {
    padding: 16px;
    gap: 24px;
  }

  .home-hero {
    padding: 24px;
    border-radius: 18px;
  }

  .home-shortcuts,
  .home-news-brief {
    padding: 24px;
    border-radius: 18px;
  }

  .shortcut-card {
    padding: 18px;
  }

  .shortcut-card__content h3 {
    font-size: 16px;
  }
}
</style>
